﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using PhanMemDoVui.DataAccess;
using PhanMemDoVui.BusinessObject;

namespace PhanMemDoVui.BusinessLogic
{
    public class ExamBUS
    {
        private Exam ex;
        private DataTable deThi;
        private static ExamBUS _instance;
#region Constructor
        protected ExamBUS()
        {
            ex = new Exam(-1, 0, "No", "", 5, 4, 3, 2);
            deThi = null;
        }
#endregion
#region Instance
        // Instance dùng thay cho hàm dựng
        public static ExamBUS Instance()
        {
            if (_instance == null)
                _instance = new ExamBUS();
            return _instance;
        }
#endregion
#region LayMaCuocThi
        public int LayMaCuocThi()
        {
            return ex.Ma;
        }
#endregion
#region LayDiem
        public int LayDiem()
        {
            if (ex.Ended.ToString() == "Yes")
                return ex.Score;
            return 0;
        }
#endregion
#region KetThucThi
        public void KetThucThi()
        {
            ex.Ended = "Yes";
            ConnectData conn = new ConnectData();
            string sql = "Select Score From Exam Where Ma = '" + ex.Ma.ToString() + "'";
            DataTable dt = conn.GetDataTable(sql);
            ex.Score = Int32.Parse(dt.Rows[0][0].ToString());
            conn.ExecuteNonQuery("Update Exam Set Ended = 'Yes' Where Ma = '" + ex.Ma.ToString() + "'");
        }
#endregion
#region LayDeThi
        public DataTable LayDeThi()
        {
            return deThi;
        }
#endregion
#region KiemTraCoCuocThiDaLuuKhong
        // nếu có cuộc thi đã lưu thì trả về mã số của cuộc thi đó, nếu không thì trả về -1
        public int Check(string Profile)
        {
            ConnectData conn = new ConnectData();
            DataTable dt = new DataTable();
            string check = "Select Ma From Exam Where Profile = '" + Profile + "' and Ended = 'No'";
            dt = conn.GetDataTable(check);
            if (dt.Rows.Count == 0)
                return -1;
            else return Int32.Parse(dt.Rows[0][0].ToString());
        }
#endregion
#region TiepTucCuocThi
        public void TaoDeThiTiepTuc(string Profile)
        {
            QuestionsBUS qt = new QuestionsBUS();
            ConnectData conn = new ConnectData();

            ex.Ma = Check(Profile);
            if (ex.Ma != -1)
            {
                string numEasy = "Select count(ExamUse.MaCH) From ExamUse, QuestnAns "
                                +"Where ExamUse.MaCH = QuestnAns.MaCH  and ExamUse.Ma = " + ex.Ma.ToString() + " "
                                +"and Difficulty = 'easy'";
                string numMode = "Select count(ExamUse.MaCH) From ExamUse, QuestnAns "
                                + "Where ExamUse.MaCH = QuestnAns.MaCH  and ExamUse.Ma = " + ex.Ma.ToString() + " "
                                + "and Difficulty = 'mode'";
                string numHard = "Select count(ExamUse.MaCH) From ExamUse, QuestnAns "
                                + "Where ExamUse.MaCH = QuestnAns.MaCH  and ExamUse.Ma = " + ex.Ma.ToString() + " "
                                + "and Difficulty = 'hard'";
                string numVhar = "Select count(ExamUse.MaCH) From ExamUse, QuestnAns "
                                + "Where ExamUse.MaCH = QuestnAns.MaCH  and ExamUse.Ma = " + ex.Ma.ToString() + " "
                                + "and Difficulty = 'vhar'";
                ex.NumEasy = ex.NumEasy - conn.ReturnANumber(numEasy);
                ex.NumMode = ex.NumMode - conn.ReturnANumber(numMode);
                ex.NumHard = ex.NumHard - conn.ReturnANumber(numHard);
                ex.NumVhar = ex.NumVhar - conn.ReturnANumber(numVhar);
                ex.Profile = Profile;
                ex.Ended = "No";
                deThi = qt.CungCapDeThi(ex.Ma, ex.NumEasy, ex.NumMode, ex.NumHard, ex.NumVhar);
            }      
        }
#endregion
#region TaoCuocThiMoi
        public void TaoCuocThiMoi(string Profile)
        {
            ConnectData conn = new ConnectData();
            QuestionsBUS qt = new QuestionsBUS();
            ex.Ma = Check(Profile);
            if (ex.Ma != -1)// neu co cuoc thi da luu
                conn.XoaExam(ex.Ma);
            ex.Ma = conn.DemSoRecord("Exam") + 1;
            ex.Score = 0;
            ex.Profile = Profile;
            ex.Ended = "No";
            // Tao mot exam moi
            string sql = string.Format("Insert into Exam values ({0}, {1}, '{2}', '{3}', {4}, {5}, {6}, {7})", ex.Ma.ToString(), ex.Score.ToString(), ex.Profile.ToString(), ex.Ended.ToString(), ex.NumEasy.ToString(), ex.NumMode.ToString(), ex.NumHard.ToString(), ex.NumVhar.ToString());
            conn.ExecuteNonQuery(sql);
            deThi = qt.CungCapDeThi(ex.Ma, ex.NumEasy, ex.NumMode, ex.NumHard, ex.NumVhar);
        }
#endregion
#region LuuCuocThi
        public bool LuuCuocThi(ExamUse eu)
        {
            ConnectData conn = new ConnectData();
            string save = string.Format("Insert into ExamUse values ({0}, {1})", eu.Ma.ToString(), eu.MaCH.ToString());
            return conn.ExecuteNonQuery(save);
        }
#endregion
    }
}
